

from rest_framework import viewsets, filters
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import mixins, viewsets
from django_filters.rest_framework import DjangoFilterBackend

from app.models import User
from app.serializers import UserSerializer
from rest_framework.pagination import PageNumberPagination
from app.auth import UserAuth

import datetime

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 2  # 每页显示10条数据
    page_size_query_param = 'page_size'  # 允许客户端通过此参数自定义每页显示数量
    max_page_size = 100  # 每页最大显示数量
    page_query_param = 'page'

#mixins.ListModelMixin,
#mixins.CreateModelMixin,
#mixins.RetrieveModelMixin,
#mixins.UpdateModelMixin,
#mixins.DestroyModelMixin,
class UserViewSet(viewsets.ModelViewSet):

    # authentication_classes = [UserAuth]
    queryset = User.objects.all()
    serializer_class = UserSerializer

    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    pagination_class = StandardResultsSetPagination


    @action(detail=False, methods=['get'])

    def total(self, request):
        """获取图书统计信息"""
        total_user = User.objects.count()
        # total_authors = User.objects.values('author').distinct().count()
        # avg_price = User.objects.aggregate(avg_price=models.Avg('price'))

        return Response({
            'total_user': total_user,
            # 'total_authors': total_authors,
            # 'average_price': avg_price['avg_price']
        })
